<template>
  <Panel tite="店铺管理">
    <el-button slot="btn" type="primary" @click="Btn"> 保存</el-button>
    <el-form label-width="80px">
      <el-form-item label="店铺头像">
        <el-upload
          :action="BASE_URL + '/shop/upload'"
          :on-success="avatarSuccess"
        >
          <img
            v-if="form.avatar"
            :src="BASE_URL + '/upload/shop/' + form.avatar"
            alt=""
          />
          <span v-else>上传</span>
        </el-upload>
      </el-form-item>

      <el-form-item label="店铺图片">
        <div class="box">
          <div class="imgg" v-for="(item, i) in form.pics" :key="item">
            <span class="span1" @click="remove(i)">x</span>
            <img :src="BASE_URL + '/upload/shop/' + item" alt="" />
          </div>
        </div>

        <el-upload 
        :action="BASE_URL + '/shop/upload'"
        :on-success="picSuccess"
        >
          <i style="font-size: 50px" class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="店铺活动">
        <el-checkbox-group v-if="form.supports" v-model="form.supports">
          <el-checkbox
            v-for="item in list"
            :key="item"
            :label="item"
          ></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
  </Panel>
</template>

<script>
import { shopInfoReq, shopEditReq } from "@/api/shop.api";

import { BASE_URL } from "@/utlis/commom.js";
export default {
  data() {
    return {
      BASE_URL,
      form: {},
      //   checkList: [],
      list: ["单人精彩套餐", "VC无限橙果汁全场8折", "情人节特价", "清明特价"],
    };
  },

  methods: {
    Btn() {
      // console.log(res);
      // 解构
      const data = { ...this.form };
      shopEditReq(data).then((res) => {
        console.log(res);
      });
    },

    // 店铺头像
    avatarSuccess(res) {
      console.log(res);
      console.log(res.imgUrl.split("/").pop());
      this.form.avatar = res.imgUrl.split("/").pop();
    },

    // 删除店铺图片 点击哪一项就删除哪一项
    remove(i) {
      this.form.pics.splice(i, 1);
    },

    // 添加图片
    picSuccess(res){
        this.form.pics.push(res.imgUrl.split("/").pop())
    },

  },

  created() {
    shopInfoReq().then((res) => {
      console.log(res);
      this.form = res.data.data;
    });
  },
};
</script>

<style lang="less" scoped>
.box {
  display: flex;
}
.imgg {
  // display: inline-block;
  position: relative;

  .span1 {
    position: absolute;
    top: -10px;
    right: 3px;
    color: red;
    font-size: 20px;
    display: none;
  }
  &:hover .span1 {
    display: block;
    cursor: pointer;
  }
}
</style>